package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.MenuDao;
import com.aaa.vegetablemarketexpress.entity.Brand;
import com.aaa.vegetablemarketexpress.entity.Menu;
import com.aaa.vegetablemarketexpress.service.MenuService;
import com.aaa.vegetablemarketexpress.util.CustomException;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.ResultStatus;
import com.aaa.vegetablemarketexpress.vo.TreeNode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @FileName: MenuServiceImpl
 * @Description: 菜单服务实现类,登录的核心逻辑，树形结构的入口
 * @Author: qxh
 * @CreateTime: 2025/9/8 9:14
 * @Version: 1.0.0
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;//菜单数据访问接口，获取数据
    /**
     * @Description: 根据用户id查询菜单
     * @Param: [userId]
     * @return: java.util.List<com.aaa.vegetablemarketexpress.vo.TreeNode>
     * @Author: qxh
     * @Date: 2025/9/8
     */
    @Override
    public List<TreeNode> queryMenuByUserId(Long userId) {
        //根据用户id查询菜单
        List<TreeNode> treeNodeList = menuDao.queryMenuByUserId(userId);
        //构建树形结构
        ArrayList<TreeNode> treeNodeListResult = new ArrayList<>();
        //判断菜单是否为空
        if(!CollectionUtils.isEmpty(treeNodeList)){
            for (TreeNode treeNode : treeNodeList) {
                //判断父级菜单是否为0
                if(treeNode.getParentId() == 0){
                    //添加到结果集合中
                    treeNodeListResult.add(treeNode);
                    bindTreeNodeChildren(treeNode,treeNodeList);
                }
            }
        }
        return treeNodeListResult;
    }
    /**
     * @Description: 查询所有菜单
     * @Param: []
     * @return: java.util.List<com.aaa.vegetablemarketexpress.vo.TreeNode>
     * @Author: qxh
     * @Date: 2025/9/8
     */
    @Override
    public List<TreeNode> queryAllMenu() {
        //获取所有菜单
        List<TreeNode> treeNodeList = menuDao.queryAllMenu();
        //构建树形结构
        ArrayList<TreeNode> treeNodeListResult = new ArrayList<>();
        //判断菜单是否为空
        if(!CollectionUtils.isEmpty(treeNodeList)){
            for (TreeNode treeNode : treeNodeList) {
                //判断父级菜单是否为0
                if(treeNode.getParentId() == 0){
                    //添加到结果集合中
                    treeNodeListResult.add(treeNode);
                    bindTreeNodeChildren(treeNode,treeNodeList);
                }
            }
        }
        return treeNodeListResult;

    }

    /**
     * 获取菜单树形结构
     * @return
     */
    @Override
    public List<TreeNode> getMenuTree() {
        // 查询所有菜单
        List<TreeNode> treeNodeList = menuDao.queryAllMenu();

        // 构建树形结构
        return buildMenuTree(treeNodeList, 0L);
    }

    private List<TreeNode> buildMenuTree(List<TreeNode> menus, Long parentId) {
        List<TreeNode> tree = new ArrayList<>();
        for (TreeNode menu : menus) {
            if (parentId.equals(menu.getParentId())) {
                List<TreeNode> children = buildMenuTree(menus,Long.valueOf(menu.getId()));
                menu.setChildren(children);
                tree.add(menu);
            }
        }
        return tree;
    }

    /**
     * @Description: 绑定子节点
     * @Param: [treeNode, treeNodeList]
     * @return: void
     * @Author: qxh
     * @Date: 2025/9/8
     */
    private void bindTreeNodeChildren(TreeNode currentTreeNode, List<TreeNode> treeNodeList) {
        for(TreeNode treeNode : treeNodeList){
            //判断当前菜单的id是否等于当前菜单的父级id
            if(currentTreeNode.getId()==treeNode.getParentId()){
                //获取子节点
                List<TreeNode> children = currentTreeNode.getChildren();
                if(children==null){
                    children = new ArrayList<>();
                }
                //添加子节点
                children.add(treeNode);
                currentTreeNode.setChildren(children);
                //递归
                bindTreeNodeChildren(treeNode,treeNodeList);
            }
        }
    }

    /**
     * 新增菜单
     * @param menu
     * @return
     */
    @Override
    public int insert(Menu menu) {
        return menuDao.insert(menu);
    }

    /**
     * 更新编辑菜单
     * @param menu
     * @return
     */
    @Override
    public int updateByPrimaryKey(Menu menu) {
        return menuDao.updateByPrimaryKey(menu);
    }

    /**
     * 根据id查询菜单
     * @param menuId
     * @return
     */
    @Override
    public Menu selectByPrimaryKey(Long menuId) {
        return menuDao.selectByPrimaryKey(menuId);
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @Override
    public PageInfo<Menu> queryByPage(Page<Menu> page) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        if (pageNo == null || pageSize == null||pageNo == 0||pageSize == 0) {
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), ResultStatus.PARAM_ISNULL.getStatusMsg());
        }
        // 调用PageHelper设置参数方法，开始分页
        PageHelper.startPage(pageNo,pageSize);
        // 获取page类的 data 属性，该属性 是Dept类型
        Menu data = page.getData();
        // 调用dao层查询
        List<Menu> menuList = menuDao.queryByParam(data);
        // 使用查询结果封装成返回对象
        return new PageInfo(menuList);
    }

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public List<Menu> selectAll() {
        return menuDao.selectAll();
    }

    /**
     * 根据父菜单id查询所有的子菜单
     * @param parentId 父菜单 ID
     * @return
     */
    @Override
    public List<Menu> getChildrenByParentId(Long parentId) {
        return menuDao.getChildrenByParentId(parentId);
    }

    /**
     * 递归删除菜单及其所有子菜单
     * @param menuId
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Long menuId) {
        // 先递归删除所有子菜单
        deleteChildrenRecursive(menuId);
        // 再删除当前菜单
        return menuDao.deleteByPrimaryKey(menuId);
    }

    /**
     * 递归删除子菜单
     * @param parentId
     */
    private void deleteChildrenRecursive(Long parentId) {
        List<Menu> children = menuDao.getChildrenByParentId(parentId);
        for (Menu child : children) {
            // 递归删除孙子菜单
            deleteChildrenRecursive(child.getMenuId());
            // 删除当前子菜单
            menuDao.deleteByPrimaryKey(child.getMenuId());
        }
    }

    /**
     * 根据父菜单ID获取完整的子树
     * @param parentId
     * @return
     */
    @Override
    public List<TreeNode> getSubTreeByParentId(Long parentId) {
        // 获取所有菜单
        List<TreeNode> allMenus = menuDao.queryAllMenu();
        // 构建指定父节点下的子树
        return buildSubTree(allMenus, parentId);
    }

    /**
     * 构建子树
     * @param menus
     * @param parentId
     * @return
     */
    private List<TreeNode> buildSubTree(List<TreeNode> menus, Long parentId) {
        List<TreeNode> tree = new ArrayList<>();
        for (TreeNode menu : menus) {
            if (menu.getParentId() == parentId) {
                List<TreeNode> children = buildSubTree(menus, Long.valueOf(menu.getId()));
                menu.setChildren(children);
                tree.add(menu);
            }
        }
        return tree;
    }
    /**
     * 根据用户编号查询该用户拥有所有的菜单(shiro)
     * @param userId
     * @return
     */
    @Override
    public List<TreeNode> queryAllMenuByUserId(Long userId) {
        return menuDao.queryMenuByUserId(userId);
    }
}














